﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCUV;
using System.Web.Configuration;
using Oracle.DataAccess.Client;

namespace sWarranty.WEB
{
    public partial class SOMSerialIncentiveCFM : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";


        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "SOMSerialMgr.aspx";

            if (_currentUserEmpNo == "")
                Response.Redirect("login.aspx");
            if (!IsPostBack)
            {

                LoadAU();
                //BuildTable(DateTime.Now.AddDays(-7), DateTime.Now, null, null, "0", null, null, "0");
            }
            btnImport.Attributes.Add("onclick", "openPopUp('SOMSerialUpload.aspx','open','400')");


        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }
        protected void LoadAU()
        {
            Entities db = new Entities();
            var au = (from t in db.TB_CM_MODEL_CAT
                      select new { au = t.PROD_L1 }).Distinct();
            ListItem i = new ListItem();
            i.Value = "0";
            i.Text = "Select";
            dropAu.Items.Add(i);
            au = au.OrderBy(k => k.au);
            foreach (var item in au)
            {
                i = new ListItem();
                i.Value = item.au;
                i.Text = item.au.ToUpper();
                dropAu.Items.Add(i);
            }
        }
        //protected void BuildTable(DateTime from, DateTime to, string shCode, string shName, string AU, string model, string suffix, string incentiveYN)
        //{
        //    //run procedure first
        //    string sdfsdfsdfsdfsdf = (RunStore("begin pk_som_mst.PR_SO_DLY_REG_INQ('" + from.ToString("yyyyMMdd") + "','" + to.ToString("yyyyMMdd") + "',null,null,null,null,null,null,null); end;"));
        //    //select from table
        //    if (string.IsNullOrEmpty(sdfsdfsdfsdfsdf))
        //    {
        //        Entities db = new Entities();
        //        var s = (from t in db.TB_XX_SO_REG_DLY
        //                 select new { sCode = t.SHOP_CODE }).Distinct();


        //        litData.Text = "";
        //        int lkjslkdjflsk = 1;//chi row dau tien dc rowspan
        //        foreach (var sh in s)
        //        {
        //            var shop = from t in db.TB_XX_SO_REG_DLY
        //                       where t.SHOP_CODE == sh.sCode
        //                       select t;
        //            if (!string.IsNullOrEmpty(shCode))
        //                shop = shop.Where(k => k.SHOP_CODE.ToUpper().Contains(shCode.ToUpper()));
        //            if (!string.IsNullOrEmpty(shName))
        //                shop = shop.Where(k => k.SHOP_NAME.ToUpper().Contains(shName.ToUpper()));
        //            if (!string.IsNullOrEmpty(model))
        //                shop = shop.Where(k => k.MODEL.ToUpper().Contains(model.ToUpper()));
        //            if (!string.IsNullOrEmpty(suffix))
        //                shop = shop.Where(k => k.SUFFIX.ToUpper().Contains(suffix.ToUpper()));
        //            if (AU != "0")
        //                shop = shop.Where(k => k.PROD_L1.ToUpper() == AU);
        //            if (incentiveYN != "0")
        //                shop = shop.Where(k => k.INCENTIVE_CFM_FLAG.ToUpper() == incentiveYN);

        //            foreach (var item in shop)
        //            {
        //                litData.Text += "<tr>";
        //                if (lkjslkdjflsk == 1)
        //                {
        //                    litData.Text += "   <td rowspan='" + shop.Count().ToString() + "'>";
        //                    litData.Text += "       <a href='#' onclick=\"openPopUp('UserRegister.aspx','popup','300')\">" + item.SHOP_CODE + "</a>";
        //                    litData.Text += "   </td>";
        //                    litData.Text += "   <td rowspan='" + shop.Count().ToString() + "'>";
        //                    litData.Text += "       " + item.SHOP_NAME + "</br> (" + DBAction.GetShopCell1(item.SHOP_CODE) + ")";
        //                    litData.Text += "   </td>";
        //                    if (item.SERIAL_NO != null)
        //                        litData.Text += "   <td>" + item.SERIAL_NO + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    string md = "";
        //                    if (item.MODEL != null)
        //                        md += item.MODEL;
        //                    if (item.SUFFIX != null)
        //                        md += "." + item.SUFFIX;
        //                    litData.Text += "   <td>" + md + "</td>";
        //                    if (item.PROD_L1 != null)
        //                        litData.Text += "   <td>" + item.PROD_L1 + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.SELLIN_DATE != null)
        //                        litData.Text += "   <td title='" + item.SELLIN_DATE.ToString() + "'>" + item.SELLIN_DATE.Value.ToString("yyyy-MM-dd") + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.SELLOUT_DATE != null)
        //                        litData.Text += "   <td title='" + item.SELLOUT_DATE.ToString() + "'>" + item.SELLOUT_DATE.Value.ToString("yyyy-MM-dd") + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.INCENTIVE_CFM_FLAG != null)
        //                    {
        //                        if (item.INCENTIVE_CFM_FLAG.ToUpper() == "N")
        //                            litData.Text += "   <td class='incentiveFlag' alt='" + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "'><span id='myspan." + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "'>N</span> <input type='checkbox' class='checkbox' id='chk." + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "'  style='display:none;'></td>";
        //                        else
        //                            litData.Text += "   <td>" + item.INCENTIVE_CFM_FLAG.ToUpper() + "</td>";
        //                    }
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.CFM_MSG != null)
        //                        litData.Text += "   <td  class='incentiveFlag' alt='" + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "'><input type='textbox' id='txt." + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "' value='" + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "' style='display:none; width:100%' > <span id='msg.xxx'>" + item.CFM_MSG.ToUpper() + "</span> </td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.INCENTIVE_CFM_DATE != null)
        //                        litData.Text += "   <td title='" + item.INCENTIVE_CFM_DATE.ToString() + "'>" + item.INCENTIVE_CFM_DATE.Value.ToString("yyyy-MM-dd") + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.CFM_USER != null)
        //                        litData.Text += "   <td>" + item.CFM_USER.ToUpper() + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                }
        //                else
        //                {
        //                    if (item.SERIAL_NO != null)
        //                        litData.Text += "   <td>" + item.SERIAL_NO + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    string md = "";
        //                    if (item.MODEL != null)
        //                        md += item.MODEL;
        //                    if (item.SUFFIX != null)
        //                        md += "." + item.SUFFIX;
        //                    litData.Text += "   <td>" + md + "</td>";
        //                    if (item.PROD_L1 != null)
        //                        litData.Text += "   <td>" + item.PROD_L1 + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.SELLIN_DATE != null)
        //                        litData.Text += "   <td title='" + item.SELLIN_DATE.ToString() + "'>" + item.SELLIN_DATE.Value.ToString("yyyy-MM-dd") + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.SELLOUT_DATE != null)
        //                        litData.Text += "   <td title='" + item.SELLOUT_DATE.ToString() + "'>" + item.SELLOUT_DATE.Value.ToString("yyyy-MM-dd") + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.INCENTIVE_CFM_FLAG != null)
        //                    {
        //                        if (item.INCENTIVE_CFM_FLAG.ToUpper() == "N")
        //                            litData.Text += "   <td class='incentiveFlag' alt='" + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "'><span id='myspan." + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "'>N</span> <input type='checkbox' class='checkbox' id='chk." + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "'  style='display:snone;'></td>";
        //                        else
        //                            litData.Text += "   <td>" + item.INCENTIVE_CFM_FLAG.ToUpper() + "</td>";
        //                    }
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.CFM_MSG != null)
        //                        litData.Text += "   <td  class='incentiveFlag' alt='" + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "'><input type='textbox' id='txt." + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX + "' value='" + item.SHOP_CODE + "." + item.SERIAL_NO + "." + item.MODEL + "." + item.SUFFIX +  "' style='display:none; width:100%' > <span id='msg.xxx'>" + item.CFM_MSG.ToUpper() + "</span> </td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.INCENTIVE_CFM_DATE != null)
        //                        litData.Text += "   <td title='" + item.INCENTIVE_CFM_DATE.ToString() + "'>" + item.INCENTIVE_CFM_DATE.Value.ToString("yyyy-MM-dd") + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                    if (item.CFM_USER != null)
        //                        litData.Text += "   <td>" + item.CFM_USER.ToUpper() + "</td>";
        //                    else
        //                        litData.Text += "<td></td>";
        //                }
        //                lkjslkdjflsk = 2;//chi chay 1 lan cho row dau tien
        //            }
        //            lkjslkdjflsk = 1;
        //        }
        //    }
        //    else
        //        MessageBox.Show(sdfsdfsdfsdfsdf);
        //}
        protected void btnFind_Click(object sender, EventArgs e)
        {
            DateTime from = Convert.ToDateTime(txtFromDate.Text);
            DateTime to = Convert.ToDateTime(txtToDate.Text);
            //BuildTable(from, to, txtShopCode.Text, txtShopName.Text, dropAu.SelectedValue, txtModel.Text, txtSuffix.Text, dropConfirmIncentive.SelectedValue);

        }
        protected string RunStore(string query)
        {
            try
            {
                //string query = "select PK_SOM_MST.FN_SN_UPLOAD_VALIDATE('EASV0sdfsd001' , 'W1943SE-PF', '103VNGT0F375', '43534534') from dual";
                String cnn = WebConfigurationManager.ConnectionStrings["tnsString"].ConnectionString;
                OracleCommand cmd =DBAction.con.CreateCommand();
                cmd.CommandText = query;
                var t = cmd.ExecuteScalar();
                if (t != null)
                    return t.ToString();
                else
                    return "";
            }
            catch (Exception ee)
            {
                return (ee.Message);
            }
        }
    }
}